package com.ys.everydaynews.ui.main.activityy

import android.text.Editable
import android.text.TextWatcher
import androidx.core.content.ContextCompat
import com.ys.everydaynews.R
import com.ys.everydaynews.base.BaseActivity
import com.ys.everydaynews.databinding.LayoutLoginPassBinding
import com.ys.everydaynews.ui.main.presenter.RegisterPresenter
import com.ys.everydaynews.ui.main.view.RegisterView
import com.ys.everydaynews.utils.CommonUtils

class LoginPassActivity : BaseActivity<RegisterView, RegisterPresenter, LayoutLoginPassBinding>(),
    TextWatcher {

    private var phoneNumber: String = ""
    private var passWoed: String = ""

    override fun getLayoutId(): Int = R.layout.layout_login_pass

    override fun initView() {
        binding.checkbox.initSpan(R.string.mmxy)
        binding.checkbox.isShow(false)
        binding.cdNext.setCardBackgroundColor(ContextCompat.getColor(this, R.color.jx_gray))
        binding.btnBack.setOnClickListener { finish() }
        binding.edtMoblie.addTextChangedListener(this)
        binding.edtPassword.addTextChangedListener(this)
    }

    override fun initData() {
    }

    override fun createPersenter(): RegisterPresenter = RegisterPresenter()

    override fun isRegister(): Boolean = false

    override fun <T> setData(data: T) {
    }

    override fun setError(err: String) {
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        phoneNumber = binding.edtMoblie.text.toString()
        passWoed = binding.edtPassword.text.toString()
        if (phoneNumber.isNotEmpty()&&passWoed.isNotEmpty()){
            checkPhone()
        }else{
            binding.cdNext.isEnabled=false
            binding.cdNext.setCardBackgroundColor(ContextCompat.getColor(this, R.color.jx_gray))
        }
    }

    override fun afterTextChanged(s: Editable?) {
    }
    private fun checkPhone() {
        if (phoneNumber.length == 13  && CommonUtils.checkPhoneNum(
                CommonUtils.replaceBlank(phoneNumber)
            )
        ) {
            binding.cdNext.isEnabled=true
            binding.cdNext.setCardBackgroundColor(ContextCompat.getColor(this, R.color.jx_red))
        } else {
            binding.cdNext.isEnabled=false
            binding.cdNext.setCardBackgroundColor(ContextCompat.getColor(this, R.color.jx_gray))
        }
    }
}